/**
 * 
 */
package snippet.tree.editor;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IValueChangeListener;
import org.eclipse.core.databinding.observable.value.ValueChangeEvent;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;

/**
 * @author amergey
 *
 */
public class EditorPage extends FormPage {
	private WritableValue value;

	public EditorPage(FormEditor editor, String id, String title) {
		super(editor, id, title);
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {
		FormToolkit toolkit = managedForm.getToolkit();
		ScrolledForm form = managedForm.getForm();
		
		//body
		form.setText("My Form Page");
		Composite body = form.getBody();
		toolkit.decorateFormHeading(form.getForm());
		GridLayout globalGridLayout = new GridLayout();
		body.setLayout(globalGridLayout);
		
		DataBindingContext context = new DataBindingContext();
		
		value = new WritableValue("",String.class);
		
		Text text = toolkit.createText(body, "",SWT.BORDER);
		text.setLayoutData(
				new GridData(SWT.FILL, SWT.TOP, true, false));
		
		context.bindValue(SWTObservables.observeText(text, SWT.Modify),value);
		value.addValueChangeListener(new IValueChangeListener() {
			
			@Override
			public void handleValueChange(ValueChangeEvent event) {
				((Editor)getEditor()).setDirty(true);
			}
		});
	}
}
